#include<iostream>
#include<vector>
using namespace std;

class Solution1 {
public:
    int mySqrt(int x) {
        if (x == 0) return 0;
        int left = 0, right = x;
        while (left <= right)
        {
            int pos = left + (right - left) / 2;
            if ((double)pos / x * pos > 1) right = pos - 1;
            else if ((double)pos / x * pos < 1) left = pos + 1;
            else return pos;
        }
        return right;
    }
};


class Solution2{
public:
    int mySqrt(int x) {
        if (x == 0) return 0;
        long left = 0, right = x;
        while (left <= right)
        {
            long pos = left + (right - left) / 2;
            if (pos * pos > x) right = pos - 1;
            else if (pos * pos < x) left = pos + 1;
            else return pos;
        }
        return right;
    }
};

class Solution3 {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 1, right = arr.size() - 2;
        while (left <= right)
        {
            int pos = left + (right - left) / 2;
            if (arr[pos] > arr[pos - 1] && arr[pos] > arr[pos + 1]) return pos;
            else if (arr[pos] > arr[pos - 1] && arr[pos] < arr[pos + 1]) left = pos + 1;
            else right = pos - 1;
        }
        return left;
    }
};

int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right)
    {
        int pos = left + (right - left) / 2;
        if (pos == 0 && nums[pos] > nums[pos + 1]) return pos;
        if (pos = nums.size() - 1 && nums[pos] > nums[pos - 1]) return pos;
        if (nums[pos] > nums[pos - 1] && nums[pos] > nums[pos + 1]) return pos;
        else if (nums[pos] > nums[pos - 1] && nums[pos] < nums[pos + 1]) left = pos + 1;
        else right = pos - 1;
    }
    return left;
}

class Solution4 {
public:
    int findPeakElement(vector<int>& nums) {
        if (nums.size() == 1) return 0;
        if (nums[0] > nums[1]) return 0;
        if (nums[nums.size() - 1] > nums[nums.size() - 2]) return nums.size() - 1;
        int left = 1, right = nums.size() - 2;
        while (left < right)
        {
            int pos = left + (right - left) / 2;
            if (nums[pos] > nums[pos - 1] && nums[pos] > nums[pos + 1]) return pos;
            else if (nums[pos] > nums[pos - 1] && nums[pos] < nums[pos + 1]) left = pos + 1;
            else right = pos - 1;
        }
        return left;
    }
};
int main()
{
    vector<int> v = { 1,2 };
    cout<<findPeakElement(v);
    return 0;
}